package com.baselibrary.utils;

import android.content.Context;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baselibrary.R;


public class HKToastUtils {

    private static CharSequence oldMsg;

    protected static Toast toast = null;

    private static final long oneTime = 0;

    private static final long twoTime = 0;

    //android系统设定的值
    private static final int LONG_DELAY = 3500; // 3.5 seconds

    private static final int SHORT_DELAY = 2000; // 2 seconds

    public static void showToast(final Context context, final CharSequence s, final int duration) {
        if ("main".equals(Thread.currentThread().getName())) {
            makeText(context.getApplicationContext(), s, duration);
        } else {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            makeText(context.getApplicationContext(), s, duration);
            Looper.loop();
        }
    }

    private static void makeText(final Context context, final CharSequence s, final int duration) {
        try {
            Toast toast = new Toast(context.getApplicationContext());
            RelativeLayout contentRl = (RelativeLayout) View.inflate(context.getApplicationContext(), R.layout.toast_common, null);
            ((TextView) contentRl.findViewById(R.id.tv_toast_common)).setText(s);
            toast.setView(contentRl);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToast(Context context, String resId) {
        showToast(context, resId, 0);
    }

    public static void showToast(int resId) {
        showToast(ContextUtils.getCurApplication(), ContextUtils.getCurApplication().getString(resId), 0);
    }

    public static void showToast(String message) {
        if (!message.equals("请重新登录")) {
            showToast(ContextUtils.getCurApplication(), message, Toast.LENGTH_SHORT);
        }
    }

    public static void showToast(Context context, int resId, int duration) {
        showToast(context.getApplicationContext(), context.getApplicationContext().getString(resId), duration);
    }

}  